package com.baizhi.config;

import com.baizhi.dao.*;
import com.baizhi.entity.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAccount;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

import static com.alibaba.druid.sql.visitor.SQLEvalVisitorUtils.eq;

/**
 * Created by Administrator on 2019/4/19 0019.
 */
public class MyRealm extends AuthorizingRealm {
    @Autowired
    private CmfzAdminDao cmfzAdminDao;
    @Autowired
    private CmfzAdminRoleDao cmfzAdminRoleDao;
    @Autowired
    private CmfzRoleDao cmfzRoleDao;
    @Autowired
    private CmfzRoleResourceDao cmfzRoleResourceDao;
    @Autowired
    private CmfzResourceDao cmfzResourceDao;

    /*配置认证信息*/
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String principal = (String) token.getPrincipal();
        Map map = new HashMap();
        map.put("admin_username", principal);
        List list = cmfzAdminDao.selectByMap(map);

        if (list != null) {
            CmfzAdmin cmfzAdmin = (CmfzAdmin) list.get(0);
            SimpleAccount simpleAccount = new SimpleAccount(cmfzAdmin.getAdminUsername(), cmfzAdmin.getAdminPassword(), ByteSource.Util.bytes(cmfzAdmin.getAdminSalt()), this.getName());
            return simpleAccount;
        } else {
            return null;
        }
    }

    /*配置授权信息*/
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        /*获取用户输入的账号*/
        String adminUsername = (String) principals.getPrimaryPrincipal();

        /*通过用户名 连接数据库 获取当前用户相关的角色信息 和 权限信息*/

        /*查admin表获取admin_id*/
        CmfzAdmin cmfzAdmin = cmfzAdminDao.selectOne(new QueryWrapper<CmfzAdmin>().eq("admin_username", adminUsername));
        Integer adminId = cmfzAdmin.getAdminId();

        /*查中间表，根据admin_id获取角色信息*/
        List<CmfzAdminRole> cmfzAdminRoles = cmfzAdminRoleDao.selectList(new QueryWrapper<CmfzAdminRole>().eq("admin_id", adminId));
        ArrayList<Integer> roleIds = new ArrayList<>();
        for (CmfzAdminRole cmfzAdminRole : cmfzAdminRoles) {
            roleIds.add(cmfzAdminRole.getRoleId());
        }

        /*查role表，获取角色信息*/
        List<CmfzRole> cmfzRoles = cmfzRoleDao.selectList(new QueryWrapper<CmfzRole>().in("role_id", roleIds));
        HashSet<String> roles = new HashSet<>();
        for(CmfzRole cmfzRole:cmfzRoles){
            roles.add(cmfzRole.getRoleName());
        }
        

        /*查role_resource表，根据roleName获取资源信息*/
        List<CmfzRoleResource> cmfzRoleResources = cmfzRoleResourceDao.selectList(new QueryWrapper<CmfzRoleResource>().in("role_name",roles));
        ArrayList<Integer> resourceIds = new ArrayList<>();
        for(CmfzRoleResource cmfzRoleResource:cmfzRoleResources){
            resourceIds.add(cmfzRoleResource.getResourceId());
        }

        /*查resource表，根据resource_id获取resource的信息*/
        List<CmfzResource> resources = cmfzResourceDao.selectList(new QueryWrapper<CmfzResource>().in("resource_id", resourceIds));
        HashSet<String> permissions = new HashSet<>();
        for(CmfzResource cmfzResource:resources){
            permissions.add(cmfzResource.getResourcePermission());
        }

        /*将信息放在info中*/
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.setRoles(roles);
        info.setStringPermissions(permissions);
        return info;
    }
}


